package offer;

public class offer_0005 {
    public String replaceSpace(String s) {
        int len=s.length();
        int num=0;
        char[] list1=s.toCharArray();
        for(int i=0;i<len;++i){
            if(list1[i]==' ') num+=1;
        }
        if(num==0) return s;
        char[] list=new char[len+num*2];
        int left=len-1;
        int right=len+2*num-1;
        while(left<right&&left>=0){
            if(list1[left]!=' '){
                list[right]=list1[left];
                right--;
                left--;
            }
            else{
                list[right--]='0';
                list[right--]='2';
                list[right--]='%';
                left--;
            }
        }
        while(left>=0){
            list[left]=list1[left];
            left--;
        }
        String result="";
        for(int i=0;i<len+2*num;++i){
            result+=list[i];
        }
        return result;
    }
}
